﻿using System;
using System.IO;
using System.Windows.Forms;
using SKQMSLib;

namespace FormGUI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            ReadFromXml.Construct();
            labelHelp.Text = ReadFromXml.HelpString;
        }
         
        #region groupBoxInput

        private bool InitialInputText = true;

        //this method clears the text that appears in the Input text box 
        //when GUI starts up
        private void ClearInitialInputText()
        {
            textBoxInput.Text = "";
            InitialInputText = false;
            textBoxInput.Focus();
        }

        private void textBoxInput_MouseClick(object sender, MouseEventArgs e)
        {
            ClearInitialInputText();
        }
         
        private void buttonClearInput_Click(object sender, EventArgs e)
        {
            textBoxInput.Clear();
            textBoxInput.Focus();
        }

        private void buttonCopyInput_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();
            
            if (textBoxInput.Text == "") MessageBox.Show("There is nothing to copy !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                Clipboard.SetText(textBoxInput.Text);
                textBoxOutput.AppendText("Equation has been copied to the clipboard !" + Environment.NewLine + Environment.NewLine);
            }
            
            textBoxInput.Focus();
        }
        
        private void buttonSolve_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();
            
            if (textBoxInput.Text == "")
            {
                MessageBox.Show("Please enter an equation !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxInput.Text = "";
                textBoxInput.Focus();
                return;
            }
            
            LinearAlgebraicEquation equation = new LinearAlgebraicEquation(textBoxInput.Text);
            textBoxSolution.Text = equation.GetSolution(true);
            textBoxOutput.AppendText(textBoxInput.Text + Environment.NewLine + textBoxSolution.Text + Environment.NewLine + Environment.NewLine);
            
            if (checkBoxLog.Checked)
            {
                StreamWriter FStreamWriter = new StreamWriter("SKQMS Log.txt", true);
                FStreamWriter.Write(textBoxInput.Text + Environment.NewLine + textBoxSolution.Text + Environment.NewLine + Environment.NewLine);
                FStreamWriter.Close();
            }
        }

        private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) buttonSolve_Click(sender, e);
        }
        
        #endregion

        #region groupBoxSolution

        private void buttonClearSolution_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();
            textBoxSolution.Clear();
            textBoxInput.Focus();
        }        
        
        private void buttonCopySolution_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();

            if (textBoxSolution.Text == "") MessageBox.Show("There is nothing to copy !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                Clipboard.SetText(textBoxSolution.Text);
                textBoxOutput.AppendText("Solution has been copied to the clipboard !" + Environment.NewLine + Environment.NewLine);
            }
            
            textBoxInput.Focus();
        }

        #endregion

        #region groupBoxOutput
       
        private void buttonClearOutput_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();
            textBoxOutput.Clear();
            textBoxInput.Focus();
        }        
        
        private void buttonCopyOutput_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();

            if (textBoxOutput.Text == "") MessageBox.Show("There is nothing to copy !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            { 
                Clipboard.SetText(textBoxOutput.Text); 
                textBoxOutput.AppendText("Output has been copied to the clipboard !" + Environment.NewLine + Environment.NewLine);
            }

            textBoxInput.Focus();
        }

        #endregion
        
        #region groupBoxTextControls

        private void buttonClearEquationAndSolution_Click(object sender, EventArgs e)
        {
            textBoxSolution.Clear();
            textBoxInput.Clear();
            textBoxInput.Focus();
        }        
               
        private void buttonCopyEquationSolution_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();
            
            if (textBoxInput.Text == "" || textBoxSolution.Text == "") MessageBox.Show("There is nothing to copy !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                Clipboard.SetText( textBoxInput.Text + Environment.NewLine + textBoxSolution.Text);
                textBoxOutput.AppendText("Equation and solution have been copied to the clipboard !" + Environment.NewLine + Environment.NewLine);
            }
            
            textBoxInput.Focus();
        }

        private void buttonClearAll_Click(object sender, EventArgs e)
        {
            textBoxInput.Clear();
            textBoxSolution.Clear();
            textBoxOutput.Clear();
            textBoxInput.Focus();
        }

        #endregion
        
        #region groupBoxLogControls

        private void checkBoxLog_CheckedChanged(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();
            
            if (checkBoxLog.CheckState == CheckState.Checked) textBoxOutput.AppendText("Logging to file started !" + Environment.NewLine + Environment.NewLine);
            else textBoxOutput.AppendText("Logging to file stopped !" + Environment.NewLine + Environment.NewLine);
            
            textBoxInput.Focus();
        }       
        
        private void buttonClearLog_Click(object sender, EventArgs e)
        {
            if (InitialInputText) ClearInitialInputText();
            
            StreamWriter FStreamWriter = new StreamWriter("SKQMS Log.txt", false);
            FStreamWriter.Write("");
            FStreamWriter.Close();
        }
        
        #endregion
    }
}